package com.onpositive.data.diskstorage;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.util.Collection;
import java.util.zip.GZIPOutputStream;

import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.IPropertyMetadata;
import com.onpositive.data.IReadableObjectCollection;
import com.onpositive.data.serializer.readers.ObjectCollectionReader;
import com.onpositive.data.serializer.readers.RandomAccessFileAccess;

public class Encoder {

	public static void main(String[] args) {
		try {
			IReadableObjectCollection<?>[] cm = new ObjectCollectionReader()
					.getCollection(new RandomAccessFileAccess(
							new RandomAccessFile(
									"C:\\work\\serialized.dat",
									"r")));
			byte[] encodeValues = encodeValues((StoredCollection) cm[0]);
			FileOutputStream d=new FileOutputStream("C:\\work\\android\\com.onpositive.metaweb.client\\src\\com\\onpositive\\metaweb\\client\\server\\data2.gzip");
			d.write(encodeValues);
			d.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static byte[] encodeValues(StoredCollection cm) {
		Collection<IPropertyMetadata> properties = cm.properties();
		try {
			ByteArrayOutputStream dd = new ByteArrayOutputStream();
			DataOutputStream os = new DataOutputStream(dd);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			ByteArrayOutputStream out1 = new ByteArrayOutputStream();
			// DataOutputStream ss = new DataOutputStream(out);
			os.writeInt(cm.size());
			os.writeLong(System.currentTimeMillis());
			os.writeInt(properties.size());
			for (IPropertyMetadata m : properties) {
				Collection<String> keys = m.keys();
				String id = m.id();
				os.writeUTF(id);
				os.writeInt(keys.size());
				for (String s : keys) {
					os.writeUTF(s);
					os.writeUTF(m.getMetaPropertyValue(s).toString());
				}
			}
			for (IPropertyMetadata m : properties) {
				if (m.id()
						.equals(IPropertyConstants.TEXT_CONTENT_PROPERTY_NAME)) {
					os.writeInt(-1);
					os.writeInt(-1);
					os.writeInt(-1);
					continue;
				}
				if (m.id().equals(
						IPropertyConstants.MAIN_PICTURE_PROPERTY_METADATA_ID)) {
					os.writeInt(-1);
					os.writeInt(-1);
					os.writeInt(-1);
					continue;
				}
				if (m.id()
						.equals(IPropertyConstants.PREVIEW_PICTURE_PROPERTY_METADATA_ID)) {
					os.writeInt(-1);
					os.writeInt(-1);
					os.writeInt(-1);
					continue;
				}
				byte[] data = cm.getData(m);
				os.write(data[0]);
				os.write(data[1]);
				os.write(data[2]);
				os.write(data[3]);
				os.writeInt(out1.size());
				os.writeInt(data.length);
				out1.write(data);
			}
			os.writeInt(out1.size());
			out1.close();
			os.write(out1.toByteArray());
			os.close();
			ByteArrayOutputStream out2 = new ByteArrayOutputStream();
			GZIPOutputStream bb = new GZIPOutputStream(out2);
			DataOutputStream dataInputStream = new DataOutputStream(bb);
			dataInputStream.writeInt(dd.size() + out.size());

			dataInputStream.write(dd.toByteArray());
			dataInputStream.write(out.toByteArray());
			dataInputStream.close();
			return out2.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
